﻿/*
 * 文件名称：
 * 程序员：yylu
 * 创建时间：2008/06/30
 * 
 * 功能说明：静态文件生成页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 *      
 * 修改记录：
 */
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using Whir.Publish;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Publish_Publish : System.Web.UI.Page
{
    bool bResponse = true;
    protected void Page_Load(object sender, EventArgs e)
    {
        //获取参数
        string columnIDs = NetRequest.GetString("columnID");      //栏目ID 以,分隔        
        string BeginDate = NetRequest.GetString("bDate");         //开始日期
        string EndDate = NetRequest.GetString("eDate");           //结束日期
        int newNum = NetRequest.GetInt("newNum", 0);              //最新记录数
        bool IsPublish = true;// NetRequest.GetInt("ispublish", 0) == 0 ? false : true;        //是否生成所有未生成的项目        
        bResponse = (NetRequest.GetInt("type", 1) == 1) ? true : false;   //是否输出js


        StopWatch sw = new StopWatch();//计算程序生成所使用的时间
        sw.start();
        if (publish(columnIDs, BeginDate, EndDate, newNum, IsPublish))
        {
            ResponseScript("<br/><br/>生成完毕！");
        }
        //else
        //{
        //    ResponseScript("生成失败！");
        //}
        float time = float.Parse(sw.elapsed().ToString()) / 1000;
        Response.Write("<script>parent.setTime('" + time.ToString() + "');</script>");
        Response.Write("<script>parent.reset();</script>");
        Response.End();
    }

    /// <summary>
    /// 生成栏目
    /// </summary>
    /// <param name="columnIDs">多个栏目ID以逗号(,)隔开</param>
    /// <returns></returns>
    public bool publish(string columnIDs, string bDate, string eDate, int num, bool isPublish)
    {
        if (columnIDs == string.Empty) return false;

        bool result = true;
        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        Whir.Model.Whir_Column[] model = null;

        //获取栏目数据
        if (columnIDs != "-1")
        {
            model = bll.GetModelList(10000, 1, "columnID in (" + columnIDs + ")");
        }
        else
        {
            //值为-1时生成全部栏目
            model = bll.GetModelList(10000, 1, "");
        }

        Response.Write("<script>parent.length=" + model.Length.ToString() + ";</script>");
        if (model != null)
        {
            try
            {
                CreateHtml index = null;
                bool btn = true;
                string[] strResult = new string[2];
                foreach (Whir.Model.Whir_Column column in model)
                {
                    //生成栏目首页
                    index = new CreateHtml(column, Page);
                    index.beginDate = bDate;
                    index.endDate = eDate;

                    ResponseScript("正在生成栏目: [" + column.columnName + "] 请稍侯...");

                    strResult = index.Bind();//一次性生成
                    btn = bool.Parse(strResult[0]);
                    if (btn)
                    {
                        if (string.IsNullOrEmpty(strResult[2]))
                        {
                            ResponseScript("没有模版页面,停止生成", strResult[2], true);
                        }
                        else
                        {
                            ResponseScript("成功 ", strResult[2], true);
                        }
                    }
                    else
                    {
                        ResponseScript(strResult[1]);
                        result = false;
                        OLAdd(strResult[1]);
                        break;
                    }
                }
            }
            catch (SystemException err)
            {
                ResponseScript(err.Message, "", true);
                result = false;
            }
        }
        return result;
    }

    /// <summary>
    /// 输出js
    /// </summary>
    /// <param name="text"></param>
    private void ResponseScript(string text)
    {
        ResponseScript(text,"" ,false);
    }
    /// <summary>
    /// 输出js
    /// </summary>
    /// <param name="text"></param>
    /// <param name="url"></param>
    private void ResponseScript(string text,string url)
    {
        ResponseScript(text, url, false);
    }
    /// <summary>
    /// 输出js
    /// </summary>
    /// <param name="text"></param>
    private void ResponseScript(string text,string url, bool bolnext)
    {
        if (bResponse)
        {
            text = text.Replace('\'', '\"').Replace('\\', '/');
            if (bolnext)
            {
                Response.Write("<script>parent.UpdateProgressBar('" + text + "','" + url + "',true);</script>");
            }
            else
            {
                Response.Write("<script>parent.UpdateProgressBar('" + text + "','" + url + "',false);</script>");
            }
            Response.Flush();
        }
    }

    /// <summary>
    /// 计算运行时间
    /// </summary>
    class StopWatch
    {
        private int mintStart;

        public void start()
        {
            mintStart = Environment.TickCount;
        }

        public long elapsed()
        {
            return Environment.TickCount - mintStart;
        }
    }

    //操作日志
    protected void OLAdd(string str)
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "发布出错";
        model.operateType = 6;
        model.operateResult = true;
        model.operateDesc = ContentManage.htmlEncode(str);

        Whir.Log.OperateLog.Add(model);
    }
}
